package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopCategory;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.ShopCategoryMapper;
import com.yami.shop.service.ShopCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 店铺分类Service实现类
 */
@Service
public class ShopCategoryServiceImpl extends ServiceImpl<ShopCategoryMapper, ShopCategory> implements ShopCategoryService {
    
    @Override
    public IPage<ShopCategory> listCategoryPage(Page<ShopCategory> page, ShopCategory shopCategory) {
        return baseMapper.listCategoryPage(page, shopCategory);
    }
    
    @Override
    public List<ShopCategory> listByShopId(Long shopId) {
        return baseMapper.listByShopId(shopId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCategory(ShopCategory shopCategory) {
        // 检查分类名称是否存在
        int count = baseMapper.checkCategoryNameExist(
                shopCategory.getShopId(), 
                shopCategory.getCategoryName(), 
                null);
        
        if (count > 0) {
            throw new YamiShopBindException("shop.category.name.exist");
        }
        
        // 设置默认值
        if (shopCategory.getSort() == null) {
            shopCategory.setSort(0);
        }
        if (shopCategory.getStatus() == null) {
            shopCategory.setStatus(1); // 默认启用
        }
        
        // 设置创建时间和更新时间
        Date now = new Date();
        shopCategory.setCreateTime(now);
        shopCategory.setUpdateTime(now);
        
        return baseMapper.insert(shopCategory) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(ShopCategory shopCategory) {
        // 检查分类是否存在
        ShopCategory existCategory = baseMapper.selectById(shopCategory.getId());
        if (existCategory == null) {
            throw new YamiShopBindException("shop.category.not.exist");
        }
        
        // 检查分类名称是否存在
        int count = baseMapper.checkCategoryNameExist(
                shopCategory.getShopId(), 
                shopCategory.getCategoryName(), 
                shopCategory.getId());
        
        if (count > 0) {
            throw new YamiShopBindException("shop.category.name.exist");
        }
        
        // 设置更新时间
        shopCategory.setUpdateTime(new Date());
        
        return baseMapper.updateById(shopCategory) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(Long id) {
        // 检查分类是否存在
        ShopCategory existCategory = baseMapper.selectById(id);
        if (existCategory == null) {
            throw new YamiShopBindException("shop.category.not.exist");
        }
        
        return baseMapper.deleteById(id) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteCategory(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    
    @Override
    public boolean updateStatus(Long id, Integer status) {
        return baseMapper.updateStatus(id, status) > 0;
    }
} 